# $Id: PKGBUILD 88238 2013-04-14 20:02:10Z aginiewicz $
# Maintainer: Andrzej Giniewicz <gginiu@gmail.com>

pkgname=mayavi
pkgver=4.3.0
pkgrel=1
pkgdesc="A 3-dimensional visualizer of scientific data"
arch=('i686' 'x86_64')
url="https://github.com/enthought/mayavi"
license=('BSD')
depends=('ipython2' 'vtk' 'python2-envisage' 'wxpython')
makedepends=('python2-distribute' 'python2-sphinx' 'xorg-server-xvfb' 'twisted')
replaces=('python2-mayavi')
provides=('python2-mayavi')
options=(!emptydirs)

source=("$pkgname-$pkgver.tar.gz::https://github.com/enthought/mayavi/archive/${pkgver}.tar.gz")
md5sums=('7708d85a77c427c8dfbeb914630d31be')

build() {
  cd "$srcdir"/mayavi-$pkgver

  # Use python2-sphinx provided sphinx-build2
  sed -i -e 's/sphinx-build/sphinx-build2/' docs/Makefile*

  # Fix distutils compatibility
  sed -i -e 's/distutils.Version/distutils.version/' mayavi/modules/text.py
  sed -i -e 's/distutils.Version/distutils.version/' tvtk/tools/mlab.py

  # set path so autodoc can find just built modules
  echo "sys.path.append('$srcdir/enthought-mayavi-${_githubtag}/build/lib.linux-$CARCH-2.7/')" >> docs/source/mayavi/conf.py
  mkdir -p build/lib.linux-$CARCH-2.7/{tvtk/plugins/scene,mayavi/preferences,mayavi/core}
  ln -s "$srcdir"/enthought-mayavi-${_githubtag}/tvtk/tvtk_classes.zip \
    build/lib.linux-$CARCH-2.7/tvtk/tvtk_classes.zip
  ln -s "$srcdir"/enthought-mayavi-${_githubtag}/mayavi/preferences/preferences.ini \
    build/lib.linux-$CARCH-2.7/mayavi/preferences/preferences.ini
  ln -s "$srcdir"/enthought-mayavi-${_githubtag}/tvtk/plugins/scene/preferences.ini \
    build/lib.linux-$CARCH-2.7/tvtk/plugins/scene/preferences.ini

  # now this is ugly, but docs do not build without X. We setup X according to:
  # http://docs.enthought.com/mayavi/mayavi/tips.html#rendering-using-the-virtual-framebuffer
  export DISPLAY=:69
  Xvfb $DISPLAY &

  # build!
  python2 setup.py build

  # have to kill the server to proceed, and do other cleanup
  kill `cat /tmp/.X${DISPLAY/:/}-lock`
  rm build/lib.linux-$CARCH-2.7/tvtk/tvtk_classes.zip
  rm build/lib.linux-$CARCH-2.7/mayavi/preferences/preferences.ini
  rm build/lib.linux-$CARCH-2.7/tvtk/plugins/scene/preferences.ini
}

package() {
  cd "$srcdir"/mayavi-$pkgver

  python2 setup.py install --root="$pkgdir"/ --optimize=1

  cp -r docs/build/* "${pkgdir}"/usr/lib/python2.7/site-packages

  sed -i -e "s|#![ ]*/usr/bin/env python$|#!/usr/bin/env python2|" \
    $(find "${pkgdir}" -name '*.py')
  sed -i -e "s|#![ ]*/usr/bin/env python$|#!/usr/bin/env python2|" \
    "$pkgdir/usr/lib/python2.7/site-packages/mayavi/tests/csv_files/csv_2_py"

  install -D LICENSE.txt "$pkgdir"/usr/share/licenses/$pkgname/LICENSE
}

